//
//  HJOAuthViewController.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/7.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SVProgressHUD

//登录需要的常量
let WB_APPKEY = "2216901993"
let WB_APP_SECRET = "486eb4fc60c964af1579795eab6fefc1"
let WB_REDIRECT_URI = "http://www.baidu.com/"

class HJOAuthViewController: UIViewController,UIWebViewDelegate {
    
    
    private lazy var webView = UIWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

        view = webView
        webView.delegate = self
        
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(WB_APPKEY)&redirect_uri=\(WB_REDIRECT_URI)"
        let url = NSURL(string: urlString)!
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
        
    }
    
    func setupUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem( title: "取消", target: self, action: "dismiss")
        navigationItem.rightBarButtonItem = UIBarButtonItem( title: "自动填充", target: self, action: "aotoFill")
        
    }
    
    //// MARK: - 监听方法
    @objc private func dismiss() {
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    @objc private func aotoFill() {
        
        let jsString = "document.getElementById('userId').value='229772017@qq.com';document.getElementById('passwd').value='123hj985'"
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
        
    }

    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }

    //webView 将要加载某个请求的时候调用,判断是否加载
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        guard let url = request.URL?.absoluteString else{
            
            return true
        }
        
        if !url.hasPrefix(WB_REDIRECT_URI) {
        
            return true
        }
            
        if request.URL!.query!.hasPrefix("code=") {
            
            let code = request.URL!.query!.substringFromIndex("code=".endIndex)
            
            HJUserAccountViewModel.sharedAccount.loadAccessToken(code) { (isSuccess) -> () in
                
                if !isSuccess {
                    
                    print("加载失败")
                    SVProgressHUD.showErrorWithStatus("网络异常,加载失败")
                    return
                }
                
                print("登录成功")
                
                SVProgressHUD.dismiss()
                
                self.dismissViewControllerAnimated(false, completion: { () -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName("HMSwitchRootVCNotifation", object: self)
                })
            }
        }else{
            //干掉当前页面
            dismiss()
        }
        
        
        return false
    }

    
    













































}
